package com.br4mmie.asciistory.battle;

import com.br4mmie.asciistory.ASCIIStoryActivity;
import com.br4mmie.asciistory.CharImage;
import com.br4mmie.asciistory.ui.Window;

/**
 * 
 * @author Niels
 * BattleWindow class, container for a battle, handles input and draws the battle to the screen
 */
public class BattleWindow extends Window {
	
	Battle battle; //represented battle
	private BattleListener listener; //Listener for this battle
	
	/**
	 * Container
	 * @param enems Array containing enemies
	 * @param background the background image for this battle
	 * @param listener this battles listener, handles victory or defeat
	 */
	public BattleWindow(EnemyType[] enems, CharImage background, BattleListener listener)
	{
		super(0, 0, ASCIIStoryActivity.getScreenWidth(), ASCIIStoryActivity.getScreenHeight());
		battle = new Battle(enems, background, ASCIIStoryActivity.getPlayer().getEncounterPlayer(), this);
		this.listener = listener;
	}
	/**
	 * Basic Loop function
	 */
	public void onLoop()
	{
		battle.onLoop();	
	}

	/**
	 * Basic handling of events
	 */
	public void handleEvents()
	{	
	}
	
	public void draw(CharImage target)
	{
		if (isClosed) return;
		battle.onDraw(image);		
		super.draw(target);
	}
	/**
	 * Victory function, called from battle on victory. takes the rewards from the battle and apsses them to the listener
	 * @param reward rewards passed from battle
	 */
	public void victory(BattleReward reward)
	{
		listener.victorious(reward);
		close();
	}
	
	/**
	 * Defeat function, called from battle, passed to listener
	 */
	public void defeat()
	{
		listener.defeated();
		close();
	}
	
	/**
	 * 
	 * @author Niels
	 *	Interface for handling victory and defeat
	 */
	public interface BattleListener
	{
		public void victorious(BattleReward reward);
		public void defeated();
	}
}
